package com.marvels.financial.loan.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fintech.third.model.shzx.PersonalDataDeletionInformation;
import com.fintech.third.service.shzx.ShzxService;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.common.utils.ServerProperties;
import com.marvels.financial.loan.dao.ShzxUploadDataDao;
import com.marvels.financial.loan.dto.ExcelInfo;
import com.marvels.financial.loan.service.ShzxDeleteDataJobApi;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

@Service
public class ShzxDeleteDataJobImpl implements ShzxDeleteDataJobApi {

	private static String orgcode = "Q10155800HOQ00";// 机构名称
	
	@Value("${config.properties.path}")
	private String configPropertiesPath;

	@Autowired
	private ShzxUploadDataDao shzxUploadDataDao;
	
	@Autowired
	private ShzxService shzxService;
	
	@Override
	public void shzxDeleteData()  throws Exception{
		MarvelsLogUtil.getInstance().info(">>>>>>>>>>>>>>>>>上海资信上传数据删除定时任务开始");
		String isShzxDataUpload = ServerProperties.getValue(configPropertiesPath, "common.properties", "is_shzx_data_delete");
		String shzxDeletePersonMobile = ServerProperties.getValue(configPropertiesPath, "common.properties", "shzx_delete_person_mobile");
		String shzxDeleteBeginDate = ServerProperties.getValue(configPropertiesPath, "common.properties", "shzx_delete_begin_date");
		if (!Boolean.parseBoolean(isShzxDataUpload) || StrUtil.isEmpty(shzxDeletePersonMobile)) {
			MarvelsLogUtil.getInstance().info(">>>>>>>>>>>>>>>>>上海资信上传数据删除开关关闭，或者必要配置缺失");
			return;
		}
		List<Map<String, Object>> personDataDeleteLists = shzxUploadDataDao.deleteDataList(Arrays.asList(shzxDeletePersonMobile.split(",")));
		ExcelInfo excelInfo = new ExcelInfo();
		List<PersonalDataDeletionInformation> personalDataDeletionInformations = new ArrayList<PersonalDataDeletionInformation>();
		String shzxDeleteEndDate = DateUtil.format(DateUtil.date(), DatePattern.PURE_DATE_PATTERN);
		if(!personDataDeleteLists.isEmpty()){
			for (Map<String, Object> map : personDataDeleteLists) {
				PersonalDataDeletionInformation personalDataDeletionInformation = new PersonalDataDeletionInformation();
				personalDataDeletionInformation.setP2pOrganizationCode(orgcode);
				personalDataDeletionInformation.setServiceNumber(map.get("loanId") == null ? "" : map.get("loanId").toString());
				personalDataDeletionInformation.setServiceBeginDate(shzxDeleteBeginDate);
				personalDataDeletionInformation.setServcieEndDate(shzxDeleteEndDate);
				personalDataDeletionInformations.add(personalDataDeletionInformation);
			}
		}
		excelInfo.setPersonalDataDeletionInformation(personalDataDeletionInformations);
		MarvelsLogUtil.getInstance().info(">>>>>>>>>>>>>>>>>上海资信上传数据删除定时任务数据组装完成");
		
		if (excelInfo != null) {
			try {
				MarvelsLogUtil.getInstance().info("===============上海资信个人数据删除上传==============" + JSONObject.toJSONString(excelInfo));
				com.fintech.third.model.shzx.ExcelInfo param = JSON.parseObject(JSONObject.toJSONString(excelInfo),
						com.fintech.third.model.shzx.ExcelInfo.class);
				shzxService.personDataDeleteUpload(param);
			} catch (Exception e) {
				MarvelsLogUtil.getInstance().error("===============上海资信个人数据删除上传异常==============", e);
			}
		}
		MarvelsLogUtil.getInstance().info("===============上海资信上传数据删除定时任务结束==============");
	}

}
